package academic.emailClient.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JPanel;

import academic.emailClient.EmailClientImpl;
import academic.emailClient.control.listeners.CancelResolveMeetingButtonActionListener;
import academic.emailClient.control.listeners.ConfirmMeetingButtonActionListener;
import academic.emailClient.control.listeners.CopyDescriptionButtonActionListener;
import academic.emailClient.control.listeners.CopyLocationButtonActionListener;
import academic.emailClient.control.listeners.CopyPurposeButtonActionListener;
import academic.emailClient.control.listeners.CreateFilterListener;
import academic.emailClient.control.listeners.CancelCreateNewMeetingActionListener;
import academic.emailClient.control.listeners.CancelCreateNewTemplateListener;
import academic.emailClient.control.listeners.CancelModifyMeetingActionListener;
import academic.emailClient.control.listeners.CancelModifyTemplateListener;
import academic.emailClient.control.listeners.ComposeMessageListener;
import academic.emailClient.control.listeners.CreateMeetingListener;
import academic.emailClient.control.listeners.CreateNewMeetingButtonActionListener;
import academic.emailClient.control.listeners.CreateNewTemplateButtonActionListener;
import academic.emailClient.control.listeners.DeclineMeetingButtonActionListener;
import academic.emailClient.control.listeners.DeleteEmailActionListener;
import academic.emailClient.control.listeners.DeleteMeetingActionListener;
import academic.emailClient.control.listeners.DeleteTemplateActionListener;
import academic.emailClient.control.listeners.ExecuteFilterListener;
import academic.emailClient.control.listeners.FilterMenuButtonMessageListener;
import academic.emailClient.control.listeners.FolderHierarchyActionListener;
import academic.emailClient.control.listeners.GetMessageListListener;
import academic.emailClient.control.listeners.InboxClientButtonActionListener;
import academic.emailClient.control.listeners.ManageFiltersComboBoxChangedListener;
import academic.emailClient.control.listeners.ManageTemplatesActionListener;
import academic.emailClient.control.listeners.ModifyMeetingButtonActionListener;
import academic.emailClient.control.listeners.ModifyMeetingTabChangedListener;
import academic.emailClient.control.listeners.ModifyMeetingsComboBoxChangedListener;
import academic.emailClient.control.listeners.ModifyTemplateActionListener;
import academic.emailClient.control.listeners.MoveToSubfolderActionListener;
import academic.emailClient.control.listeners.ResolveMeetingButtonActionListener;
import academic.emailClient.control.listeners.SearchMessageListener;
import academic.emailClient.control.listeners.SearchingTheMessageListener;
import academic.emailClient.model.EmailClientModel;
import academic.emailClient.view.ComposeMailView;
import academic.emailClient.view.DynamicTree;
import academic.emailClient.view.EmailClientViewInterface;

/**
 * This class is the controller that control the interaction between
 * our view interface and our model interface 
 *
 */

public class EmailClientController{
	
	/**
	 * Access to Model and View methods public properties
	 * 
	 */
	
	private EmailClientViewInterface view;
	private EmailClientModel model;
	
	/**
	 * Constructor
	 * @param view
	 * @param model
	 * @throws Exception 
	 */
	public EmailClientController(EmailClientViewInterface view, EmailClientModel model) throws Exception{
		this.view = view;
		this.model = model;
		FolderViewMapper mapper = new FolderViewMapper(model.getFoldersHierarchy());
		DynamicTree folders = mapper.map();
		HashMap<String, String> paths = model.getAllSubfoldersPaths();
		//view.updateMailBoxList(MessageMapper.mapToMessageViewList(model.getIboxMessages()));
		
		
		/**
		 *  Tell the View that whenever the Compose button
		 *  is clicked to execute the actionPerformed method
		 *   in the EmailClientControllerListener inner class
		 *   this.view.ComposeEmailButtonActionListener(new ComposeEmailClientControllerListener());
		 */

		this.view.setFoldersHierarchy(folders);
		this.view.setSubfoldersPath(paths);
		this.view.display();
		this.view.inboxButtonActionListener(new InboxClientButtonActionListener(view, model));
		this.view.addComposeMessageListener(new ComposeMessageListener(view, model));
		this.view.confirmMeetingActionListener(new ConfirmMeetingButtonActionListener(view, model));
		this.view.declineMeetingActionListener(new DeclineMeetingButtonActionListener(view, model));
		this.view.addTemplatesManagerListener(new ManageTemplatesActionListener(view, model));
		this.view.addModifyFIlterMenyButtonManagerListener(new FilterMenuButtonMessageListener(view, model));
		this.view.addTemplatesManagerListener(new ManageTemplatesActionListener(view, model));
		this.view.AddModifyMeetingTabChangedListener(new ModifyMeetingTabChangedListener(view, model));
		this.view.addCreateMeetingListener(new CreateMeetingListener(view, model));
		this.view.addCreateMeetingButtonListener(new CreateNewMeetingButtonActionListener(view, model));
		this.view.deleteEmailButtonActionListener(new DeleteEmailActionListener(view, model));
		this.view.addGetMessagesListener(new GetMessageListListener(view, model));
		this.view.addFolderBehavior(new FolderHierarchyActionListener(view, model));
		this.view.addCancelCreateNewTemplatesListener(new CancelCreateNewTemplateListener(view, model));
		this.view.addCreateNewTemplatesListener(new CreateNewTemplateButtonActionListener(view, model));
		this.view.addCancelModifyTemplatesListener(new CancelModifyTemplateListener(view, model));
		this.view.addMoveToSubfolderListener(new MoveToSubfolderActionListener(model, view));
		this.view.addSearchMessageListener(new SearchMessageListener(view, model));
		view.addModifyTemplatesListener(new ModifyTemplateActionListener(view, model));
		view.addDeleteTemplatesListener(new DeleteTemplateActionListener(view, model));	
		view.addModifyMeetingCbListener(new ModifyMeetingsComboBoxChangedListener(view, model));
		view.addModifyMeetingButtonActionListener(new ModifyMeetingButtonActionListener(view, model));
		view.addDeleteMeetingButtonActionListener(new DeleteMeetingActionListener(view, model));
		view.AddCancelModifyMeeting(new CancelModifyMeetingActionListener(view, model));
		view.AddCancelCreateMeetingButtonActionListener(new CancelCreateNewMeetingActionListener(view, model));
		view.addTheFilterListener(new CreateFilterListener(view,model));
		view.addExecuteFilterListener(new ExecuteFilterListener(view, model));		
		view.addCopyDescriptionButtonActionListener(new CopyDescriptionButtonActionListener(view, model));
		view.addCopyPurposeButtonActionListener(new CopyPurposeButtonActionListener(view, model));
		view.addCopyLocationButtonActionListener(new CopyLocationButtonActionListener(view, model));
		view.addResolveMeetingButtonActionListener(new ResolveMeetingButtonActionListener(view, model));
		view.addCancelResolveMeetingButtonActionListener(new CancelResolveMeetingButtonActionListener(view, model));	
	}
	
			
		/**
		 * This class is used to control the functionality of the compose button
		 * by opening a the new message window
		 * @author pc
		 *
		 */
	    public class ComposeEmailClientControllerListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			 ComposeMailView  composeFrame=new ComposeMailView();
			  /**
			   * to add the compose message view to the current view
			   */
			  view.updateInboxList(composeFrame.getPanel());			
			}
	   }
		/**
		 * this class is used to control the functionality of the Inbox button
		 * to open display the Inbox contents when clicking on the Inbox button
		 * @author pc
		 *
		 */
			public class InboxControllerListener implements ActionListener{		
				@Override
				public void actionPerformed(ActionEvent arg0) {
					view.updateInboxList(new JPanel());					
				}
			}
	}
